package cn.tedu.egweibo3.controller;

import cn.tedu.egweibo3.common.response.JsonResult;
import cn.tedu.egweibo3.common.response.StatusCode;
import cn.tedu.egweibo3.mapper.CommentMapper;
import cn.tedu.egweibo3.pojo.dto.CommentDTO;
import cn.tedu.egweibo3.pojo.entity.Comment;
import cn.tedu.egweibo3.pojo.vo.CommentVO;
import cn.tedu.egweibo3.pojo.vo.UserVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "评论模块")
@RestController
@RequestMapping("/v1/comment/insert")
public class CommentController {

    @Autowired
    private CommentMapper commentMapper;

    //发布评论
    @ApiOperation(value = "发布评论")
    @PostMapping("insert")
    public JsonResult insertComment(@RequestBody CommentDTO commentDTO, @ApiIgnore HttpSession session){
       UserVO userVO =(UserVO) session.getAttribute("user");
       if (userVO==null){
           return new JsonResult(StatusCode.NOT_LOGIN);
       }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(userVO.getId());
        commentMapper.insertComment(comment);
        return new JsonResult(StatusCode.OPERATION_SUCCESS);
    }

    //获取评论列表
    @ApiOperation(value = "获取评论列表")
    @GetMapping("selectByweiboId")
    public JsonResult selectByWeiboId(int id){
        List<CommentVO> commentVOS =commentMapper.selectByWeiboId(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS,commentVOS);

    }

}
